class ReservationsController < ApplicationController
  # GET /reservations
  # GET /reservations.json
  def index
    @reservations = Reservation.all

    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @reservations }
    end
  end

  # GET /reservations/1
  # GET /reservations/1.json
  def show
    @reservation = Reservation.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @reservation }
    end
  end

  # GET /reservations/new
  # GET /reservations/new.json
  def new
    @reservation = Reservation.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @reservation }
    end
  end

  # GET /reservations/1/edit
  def edit
    @reservation = Reservation.find(params[:id])
  end

  # POST /reservations
  # POST /reservations.json
  def create
    @reservation = Reservation.new(params[:reservation])

    respond_to do |format|
      if @reservation.save
        format.html { redirect_to(@reservation, :notice => 'Reservation was successfully created.') }
        format.json  { render :json => @reservation, :status => :created, :location => @reservation }
      else
        format.html { render :action => "new" }
        format.json  { render :json => @reservation.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /reservations/1
  # PUT /reservations/1.json
  def update
    @reservation = Reservation.find(params[:id])

    respond_to do |format|
      if @reservation.update_attributes(params[:reservation])
        format.html { redirect_to(@reservation, :notice => 'Reservation was successfully updated.') }
        format.json  { head :ok }
      else
        format.html { render :action => "edit" }
        format.json  { render :json => @reservation.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /reservations/1
  # DELETE /reservations/1.json
  def destroy
    @reservation = Reservation.find(params[:id])
    @reservation.destroy

    respond_to do |format|
      format.html { redirect_to(reservations_url) }
      format.json  { head :ok }
    end
  end

  def create_res

    @reservation = Reservation.new
    @reservation.rdate = params[:rdate] #'2012/12/20'
    @reservation.rtime = params[:rtime] #'13:00:00'
    @reservation.qty = params[:qty] #1
    @reservation.table_id = params[:table_id] #1
    @reservation.qtytable = params[:qtytable] #1
    @reservation.status = params[:status] #1
    @reservation.restaurant_id = params[:rest_id]
    @reservation.restaurant_name = Restaurant.find(params[:rest_id]).name
    @reservation.local_id = params[:local_id]
    @reservation.user_id = params[:user_id]
    @reservation.user = User.find(params[:user_id]).name
    @reservation.local_name = Local.find(params[:local_id]).name
    @reservation.save
    
    @commentary = Commentary.new
    @commentary.reservation_id = @reservation.id
    @commentary.user_id = params[:user_id]
    @commentary.usrname = User.find(params[:user_id]).name
    @commentary.cmt = ''
    @commentary.rate = 3
    @commentary.rest_rate = 3
    @commentary.local_rate = 3
    @commentary.local_id = params[:local_id]
    @commentary.restaurant_id = params[:rest_id]
    @commentary.status = 1
    @commentary.save

    @strElegidos = params[:dishes].strip
    @list_dishes = @strElegidos.split('-')

    @list_dishes.each do |d|

      @dscore = Dscore.new
      @dscore.rate = 1
      @dscore.status = 1
      @dscore.user_id = @commentary.user_id
      @dscore.user_name = @commentary.usrname
      @dscore.dish_id = Integer(d)
      @dscore.resid = @reservation.id
      @dscore.save
        
    end

    respond_to do |format|
      format.json  { render :json => @reservation}
    end
  end

  def getResByUserId
    @reservations = Reservation.where(:user_id => params[:user_id])
    respond_to do |format|
      format.json  { render :json => @reservations }
    end
  end

  def getResByRestLoc

    @Rest_id = params[:rest_id]
    @Local_id = params[:local_id]

    @reservations = Reservation.where(:restaurant_id => params[:rest_id], :local_id => params[:local_id], :status => 1)

    respond_to do |format|
      format.json  { render :json => @reservations }
    end

  end

  def confirmRes

    @reservation = Reservation.find (params[:res_id])
    @reservation.status = 2
    @reservation.save

    respond_to do |format|
      format.json  { render :json => @reservation }
    end
  end

end
